module.controller('SelectLogConditionController',
    function ($scope,$rootScope,$state,$location, $stateParams,k8sService) {
        $scope.clusterId = $stateParams.clusterId;
        $scope.serviceName = $stateParams.serviceName;
        $scope.svc = $stateParams.svc;
        $scope.isHide = true;
        $scope.startDate = new Date();
        $scope.endDate = new Date();

        $scope.minDate = new Date(
            $scope.startDate.getFullYear(),
            $scope.startDate.getMonth() - 1,
            $scope.startDate.getDate()
        );
        $scope.maxDate = new Date(
            $scope.startDate.getFullYear(),
            $scope.startDate.getMonth(),
            $scope.startDate.getDate()
        );

        $scope.startHour = '';
        $scope.startHours = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24').split(' ').map(function (state) {
            return {abbrev:state};
        });
        $scope.startMinute = '';
        $scope.startMinutes = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60').split(' ').map(function (state) {
            return {abbrev:state};
        });
        $scope.startSecond = '';
        $scope.startSeconds = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60').split(' ').map(function (state) {
            return {abbrev:state};
        });

        // $scope.endHour = '';
        // $scope.endHours = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24').split(' ').map(function (state) {
        //     return {abbrev:state};
        // });
        // $scope.endMinute = '';
        // $scope.endMinutes = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60').split(' ').map(function (state) {
        //     return {abbrev:state};
        // });
        // $scope.endSecond = '';
        // $scope.endSeconds = ('01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60').split(' ').map(function (state) {
        //     return {abbrev:state};
        // });
        // $scope.lines = '';
        // $scope.numbers = ('all 5 10 20 30 40 50 60 70 80 90 100').split(' ').map(function (line) {
        //     return {abbrev:line};
        // });
        function change(time) {
            if(time < 10){
                time = '0' + time;
            }
            return time;
        }
        var replicasname = $stateParams.serviceName;
        var clusterId = $stateParams.clusterId;

        $scope.startCheck = function () {
            $scope.currentPageRunInfos = [];
            $scope.isHide = false;
            var year = $scope.startDate.getFullYear();
            var month = $scope.startDate.getMonth() + 1;
            var day = $scope.startDate.getDate();
            var hour = $scope.startHour - 8;
            var minute = $scope.startMinute;
            var second = $scope.startSecond;
            if(hour < 0){
                hour = hour + 24;
                day = day - 1;
            }
            if(day <= 0){
                switch (month - 1){
                    case 1,3,5,7,8,10,12:
                        day = day + 31;
                        month = month - 1;
                        if(month === 0){
                            month = month + 12;
                            year = year - 1;
                        }
                        break;
                    case 4,6,9,11:
                        day = day + 30;
                        month = month - 1;
                        break;
                    case 2:
                        if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0 ){
                            day = day + 29;
                        }else{
                            day = day + 28;
                        }
                }
            }
            if(hour === ''){
                hour = '00';
                minute = '00';
                second = '00';
            }
            if (minute === ''){
                minute = '00';
                second = '00';
            }
            if(second === ''){
                second = '00';
            }
            month = change(month);
            day = change(day);
            var sinceTime = year+'-'+month+'-'+day+'T'+hour+':'+minute+':'+second+'Z';
            k8sService.getPodLog(replicasname,clusterId,sinceTime).then(function(response){
                if (response.data === '' || response.data === null){
                    $scope.isHide = true;
                    $scope.currentPageRunInfos = ['查询结果为空'];
                }else {
                    var lines = response.data.split('\n')
                    for(var i = 0; i < lines.length; i++){
                        if(lines[i] === "" || lines[i] === " "){
                            lines.splice(i,1);
                        }
                    }

                    var datas = [];
                    for (var i = 0; i < lines.length / 40; i ++){
                        var testArrOne = lines.slice(i * 40, i * 40 + 40);
                        datas[i] = testArrOne;
                    }

                    var infos = [];
                    for(var i = 0; i < datas.length; i++){
                        var string = JSON.stringify(datas[i].join('\n'));
                        infos[i] = JSON.parse(string);
                    }

                    $scope.line_number = infos.length;
                    $scope.currentPageRunInfos = [];
                    $scope.currentPage = 1;
                    $scope.numPerPage = 1;
                    $scope.$watch("infos + currentPage", function() {
                        if (infos) {
                            var begin = (($scope.currentPage - 1) * $scope.numPerPage)
                                , end = begin + $scope.numPerPage;
                            $scope.currentPageRunInfos = infos.slice(begin, end);
                        }
                    });
                    $scope.isHide = true;

                }

            },function (response) {
                alertify.error(response.data);
            });
        }

    });